package com.example.tabhostdemo;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

	private TabHost tabhost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tabhost = getTabHost();
		
		TabSpec tabSpec1 = buildTag("first", "第一个", R.drawable.first, FirstActivity.class);
		tabhost.addTab(tabSpec1);
		TabSpec tabSpec2 = buildTag("second", "第二个", R.drawable.second, SecondActivity.class);
		tabhost.addTab(tabSpec2);
		TabSpec tabSpec3 = buildTag("third", "第三个", R.drawable.third, ThirdActivity.class);
		tabhost.addTab(tabSpec3);
		
		/*
		 *更改tab的背景
		 *
		final TabWidget tabWidget = tabhost.getTabWidget();
		for(int i=0;i<tabWidget.getChildCount();i++){
			View v = tabWidget.getChildAt(i);
			v.setBackgroundResource(R.drawable.tab_pg);
			TextView tv = (TextView) v.findViewById(android.R.id.title);
			tv.setTextSize(16);
			tv.setTextColor(Color.WHITE);
			if(tabhost.getCurrentTab() == i){
				v.setBackgroundResource(R.drawable.tab_bg);
			}
		}
		
		tabhost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				//把所有tab背景初始化
				for(int i=0;i<tabWidget.getChildCount();i++){
					View v = tabWidget.getChildAt(i);
					v.setBackgroundResource(R.drawable.tab_pg);
					if(tabhost.getCurrentTab() == i){
						v.setBackgroundResource(R.drawable.tab_bg);
					}
				}
			}
		});
		*/
	}

	private TabSpec buildTag(String tag, String title, int res, Class cls) {
		TabSpec tabSpec = tabhost.newTabSpec(tag);
		tabSpec.setIndicator(null, getResources().getDrawable(res));
		tabSpec.setContent(new Intent(MainActivity.this, cls));
		return tabSpec;
	}
}
